#!/usr/bin/perl -w

#=======================================================================
# xml-to-lisp
# File ID: e6461d24-5d49-11df-8e5d-90e6ba3022ac
# Converts an XML file to Lisp.
# Created by Gene Michael Stover (gene at acm.org)
# Tweaks by Øyvind A. Holm <sunny@sunbase.org>.
#=======================================================================

$| = 1;

use strict;
use XML::Parser;
use Data::Dumper;

my $indent = 0;

sub print_indent () {
    foreach my $i (1 .. $indent) {
        print " ";
    }
}

###
### Callback functions.
### "cb" stands for "call back".
###

sub cb_start {
    my $expat = shift;
    my $element = shift;
    my @attrval = (@_);
    print "\n";
    print_indent;
    print "((";
    print $element;
    foreach my $i (0 .. $#attrval) {
        if ($i % 2 == 0) {
            print " (", $attrval[$i];
        } else {
            print ' . "', $attrval[$i], '")';
        }
    }
    print ")";
    $indent += 2;
}

sub cb_end {
    my $expat = shift;
    my $element = shift;
    $indent -= 2;
    print ")";
}

sub cb_char {
    my $expat = shift;
    my $str = shift;
    chomp $str;
    if (length $str > 0) {
        print " \"";
        print $str;
        print '"';
    }
}

###
### Main
###

my $xml = new XML::Parser (Handlers => {Start => \&cb_start,
                           End      => \&cb_end,
                           Char     => \&cb_char});
if ($xml->parse (join "", <>)) {
} else {
    print "error: XML parsefile failed\n";
    exit 1;
}

### --- end of file ---
